<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/30
 * Time: 17:16
 */

namespace App\Model\Backstage;


use App\Model\Base;
use Illuminate\Support\Facades\DB;

class CustomersFollow extends Base
{
    public $customersfollow;
    public $customersource;
    public function __construct()
    {
        parent::__construct();
//        $this->customersfollow = 'customer_follow'.$this->branch_id;
        $this->customersfollow = 'customer_follow';
        $this->customersource = 'customer_follow_source';
    }

    /**
     * @param $cid 客源ID
     * @param $cf_fllow_type  跟进方式
     * @param $cf_content  跟进内容
     * @param $cf_room_id  跟进同时 同步的房源ID
     * @return mixed
     */
    public function addCustomersFollow($cid, $cf_fllow_type, $cf_content, $cf_room_id)
    {
//        return DB::table($this->customersfollow)
        return DB::table('customer_follow')
            ->insertGetId([
                'cf_c_id'       => $cid,
                'cf_fllow_type' => $cf_fllow_type,
                'cf_content'    => $cf_content,
                'cf_room_id'    => isset($cf_room_id) ? $cf_room_id : '',
                'cf_user_id'    => $this->uid,
                'cf_created_at' => time()
            ]);
    }


    /**
     * @param array $map
     * @param $pageLimit
     * @return mixed
     * 获取用户客源跟进数量排行
     * author:lxy
     */
    public function getCustomersFollowNum($map = [],$pageLimit){
        $where = [
            'u_b_id' => $this->branch_id,
            'is_del' => 0,
            'u_status' => 1
        ];
        $user_list = DB::table('user')
            ->where($where)
            ->select(['u_id','u_username','u_type'])
            ->get()
            ->toArray();
        foreach($user_list as $k => $v){
            $num = DB::table($this->customersfollow)->where($map)->where('cf_user_id', $v->u_id)->count();
            $v->num = $num;
        }
        $list = json_encode($user_list);
        $list = json_decode($list,true);
        array_multisort(array_column($list,'num'),SORT_DESC,$list);
        return $list;

    }

    /*
     * 删除客源跟进
     * Wu Ning
     */

    public function delFollow($param)
    {
        DB::table($this->customersource)->where('cf_id', $param)->delete();

       return DB::table($this->customersfollow)->where('cf_id', $param)->delete();
    }

    /**
     * @param $roomInfoModel
     * @param $params
     * @return mixed
     * 移动员工客源跟进
     */
    public function moves($customersModel, $params){
        $cus_ids = $customersModel->moves($params, 2);
        return DB::table($this->customersfollow)
            ->whereIn('cf_c_id',$cus_ids )
            ->update([
                'cf_user_id' => $params['to_uid']
            ]);
    }













}